<?php

  require_once dirname( dirname( __FILE__ ) ) . '/ilib.php';
  /*
    class Task {

    var $data;
    var $id;

    function __construct ( $row, $id ) {
    $this->id = $id;
    $this->parse( $row );
    }

    function parse ( $data ) {

    $data[1] = explode_with_trim( "\n", $data[1] );
    $data[2] = explode_with_trim( ",", $data[2] );
    $data[8] = explode_with_trim( ",", $data[8] );
    $this->data = $data;
    }

    function isCorrect () {
    return count( $this->data[1] ) > 0 && count( $this->data[2] ) > 0;
    }

    function getRate () {
    return $this->data[6];
    }

    function rateAnswer ( $answer ) {
    foreach ( $this->data[1] as $id => $val ) {
    $right = in_array( $id, $answer ) xor in_array( $id, $this->data[2] );
    $sum += $right;
    //if($an)
    }
    //echo $sum.'/'.count($this->data[1]);

    return $sum / count( $this->data[1] ) * $this->data[6];
    }

    }
   */

  /**
   * Для работы с данными из базы
   */
  interface ITask {

      function getId ();

      function getQuestion ();

      function getAnswers ();

      function getCorrectAnswers ();

      function getExterndedAnswer ();

      function isWeakAnswer ();

      function getWeight ();

      function getTime ();

      function getTags ();

      function getModuleId ();
  }

  class Task implements ITask {

      var $data;

      function getId () {
          return $this->data['id'];
      }

      function getQuestion () {
          return $this->data['question'];
      }

      function getAnswers () {
          return $this->data['answers'];
      }

      function getCorrectAnswers () {
          return $this->data['correct_answers'];
      }

      function getExterndedAnswer () {
          return $this->data['extended_answer'];
      }

      function isWeakAnswer () {
          return $this->data['weak'];
      }

      function getWeight () {
          return $this->data['weight'];
      }

      function getTime () {
          return $this->data['time'];
      }

      function getTags () {
          return $this->data['tags'];
      }

      function getModuleId () {
          return $this->data['module_id'];
      }

  }

  class RealTask extends Task {

      function setId ( $id ) {
          $this->data['id'] = $id;
      }

      function setModuleId ( $id ) {
          $this->data['module_id'] = $id;
      }

      function parseArray ( $data, $id = 0 ) {
          $this->data = array( );
          $this->data['id'] = $id;
          $this->data['question'] = trim( $data[0] );
          //TODO Убрать 1. 2.
          if ( strpos( $data[1], '!@#' ) !== false )
              $this->data['answers'] = explode_with_trim( "!@#", $data[1] );
          else
              $this->data['answers'] = explode_with_trim( "\n", $data[1] );

          $correct_answers = explode_with_trim( ",", $data[2] );

          $correct_answers_ids = array( );
          if ( $correct_answers )
              foreach ( $correct_answers as $correct_answer ) {
                  if ( $correct_answer != 0 )
                      $correct_answers_ids[] = $correct_answer - 1;
              }
          $this->data['correct_answers'] = $correct_answers_ids;
          $this->data['extended_answer'] = trim( $data[8] );
          $this->data['weak'] = trim( $data[3] );
          $this->data['weight'] = trim( $data[4] );
          $this->data['time'] = trim( $data[5] );
          $this->data['tags'] = explode_with_trim( ",", $data[6] );
          $this->data['module_id'] = trim( $data[7] );
      }

      function getQuery () {
          $data = $this->data;
          //unset($data['id']);
          $data['json'] = $this->getJson();
          foreach ( array( 'answers', 'correct_answers', 'tags' ) as $field ) {
              $data[$field] = json_encode( $data[$field] );
          }

          $query = 'INSERT INTO  `host1000494_pub`.`cont_questions` (' .
                  array2string( array_keys( $data ), '`', ', ' ) .
                  ') VALUES (' .
                  array2string( $data, '\'', ', ' ) .
                  ')';
          return $query;
      }

      function save () {
          $this->getQuery();
          return mysql_query( $this->getQuery() );
      }

      function load ( $row ) {
          $this->setJson( $row['json'] );
      }

      function getJson () {
          return my_json_encode( $this->data );
      }

      function setJson ( $json ) {
          $this->data = my_json_decode( $json );
      }

      function isCorrectTask () {
          return count( $this->getAnswers() ) > 0 && count( $this->getCorrectAnswers() ) > 0;
      }

      function countCorrectAnswer ( $answers ) {
          $sum = 0;
          foreach ( $this->data['answers'] as $id => $val ) {
              if ( in_array( $id, $answers ) xor in_array( $id, $this->data['answers'] ) )
                  $sum += 1;
          }
          return $sum;
      }

      function rateAnswer ( $answers ) {
          $count = $this->countCorrectAnswer( $answers );

          if ( $this->isWeakAnswer() ) {
              return $count / count( $this->data['answers'] ) * $this->data['weight'];
          } else {
              return count( $this->data['answers'] ) == $count ? $this->data['weight'] : 0;
          }
      }

  }

?>
